package logic

import (
	"context"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/utils/database"
	"grpc.getaway.adminspro.padmins.com/pkg/config/businessError"
	"strings"
)

func Permission(ctx context.Context, resourceIds []int64, resource string) (bool, *businessError.ErrorInfo) {

	for _, resourceId := range resourceIds {
		if resourceId == int64(0) {
			return true, nil
		}
	}

	row, e := database.Db.GetDefaultDb(ctx).Find("select * from admin_resource where resource=? or path=?", resource, strings.ReplaceAll(resource, ".", "/"))
	if e != nil {
		return false, businessError.DbError(e)
	}

	if row == nil {
		return false, nil
	}

	for _, resourceId := range resourceIds {
		if resourceId == row["id"].(int64) {
			return true, nil
		}
	}

	return false, nil
}
